{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3f71b03e",
   "metadata": {},
   "source": [
    "# 面向对象常用方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d27cdef",
   "metadata": {},
   "source": [
    "一、理解\n",
    "\n",
    "1、\\__new__()是在新式类中新出现的方法，它作用在构造方法建造实例之前。\n",
    "\n",
    "　　即可以这样理解：Python中存在于类中的构造方法\\__init__（）负责将类实例化，而在\\__init__（）执行之前，\\__new__（）负责制造这样的一个实例对象，以便\\__init__（）去让该实例对象更加的丰富（为其添加属性等）。\n",
    "\n",
    "　　同时：\\__new__() 方法还决定是否要使用该\\__init__() 方法，因为\\__new__()可以调用其他类的构造方法或者直接返回别的对象来作为本类 的实例。\n",
    "\n",
    "　　2、\\__new__() 方法始终都是类的静态方法，即使没有被加上静态方法装饰器。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b39c97c4",
   "metadata": {},
   "source": [
    "二、结构\n",
    "　　\n",
    "\n",
    "在Python中，若不重写类中的\\__new__()方法，其默认的结构时这样的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "87a85bc1",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Foo(object):\n",
    "    def __new__(cls,*args,**kwagrs):\n",
    "        return super().__new__(cls,*args,**kwagrs)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9c6a3c4",
   "metadata": {},
   "source": [
    "　　1、其第一个参数 cls 是当前正在实例化的类。　\n",
    "\n",
    "　　如果要得到当前类的实例，应当在当前类中的\\__new__()方法语句中调用当前类的父类 的\\__new__()方法。例如，如果当前类是直接继承自object，那当前类的__new__()方法返回的对象应该为："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bb917809",
   "metadata": {},
   "outputs": [],
   "source": [
    "class A(object):\n",
    "    pass\n",
    "\n",
    "class B(A):\n",
    "    pass\n",
    "\n",
    "class C(B):\n",
    "    def __new__(cls,*args,**kwargs):\n",
    "        return super().__new__(cls, *args,**kwargs)\n",
    "\n",
    "        # 等同于：\n",
    "        # return B.__new__(cls, *args,**kwargs)\n",
    "        # return A.__new__(cls, *args,**kwargs)\n",
    "        # return object.__new__(cls, *args,**kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "90e08c6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class '__main__.Foo'>\n"
     ]
    }
   ],
   "source": [
    "class Foo(object):\n",
    "    def __new__(cls,*args,**kwagrs):\n",
    "        return object.__new__(cls,*args,**kwagrs)\n",
    "    \n",
    "    def __init__(self,name):\n",
    "        self.name = name\n",
    "\n",
    "class Bar(object):\n",
    "    def __new__(cls,*agrs,**kwagrs):\n",
    "        return object.__new__(Foo,*agrs,**kwagrs)\n",
    "    \n",
    "bar = Bar()\n",
    "print(type(bar)) #foo其实是Stranger类的实例。\n",
    "\n",
    "# 输出为：<class '__main__.Foo'>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f934dc97",
   "metadata": {},
   "source": [
    "\\__call()__  Python中，只要在创建类型的时候定义了__call__()方法，这个类型就是可调用的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "848115d8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c49ca9f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "\\__str__,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "627f17cd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
